package ij.umasuo.gui.algorithm;

import ij.IJ;
import ij.umasuo.MyConstants;

import java.awt.RenderingHints.Key;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

/**
 * the algorithm editor used to editor the algorithm which we used to process the images
 * @author liuquan
 * @date 2013-08-17
 * @version 1.0
 *
 */
public class AlgorithmEditor extends JPanel implements MouseListener, KeyListener{

	private static final long serialVersionUID = 1L;
	private static AlgorithmEditor instance = null;
	
	private JCheckBox centerCB = new JCheckBox("Center Of Mass");
	private JCheckBox gaussianCB = new JCheckBox("Gaussian Fitting");
	private JCheckBox is3DCB = new JCheckBox("is3D");
	private JTextField psfTF = new JTextField();
	private JTextField pfaTF = new JTextField();
	private final JLabel lblPw = new JLabel("PW :");
	private final JTextField pwTF = new JTextField();
	
	private AlgorithmEditor(){
		setLayout(null);
		
		
		centerCB.addMouseListener(this);
		centerCB.setBounds(6, 7, 156, 23);
		add(centerCB);
		
		gaussianCB.addMouseListener(this);
		gaussianCB.setBounds(6, 33, 156, 23);
		add(gaussianCB);
		is3DCB.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				if(is3DCB.isSelected()){
					MyConstants.IS_3D = true;
				}else{
					MyConstants.IS_3D = false;
				}
			}
		});
		

		is3DCB.setBounds(6, 71, 97, 23);
		add(is3DCB);
		
		
		psfTF.setBounds(58, 120, 86, 20);
		add(psfTF);
		psfTF.addKeyListener(this);
		psfTF.setColumns(10);
		
		JLabel lblPsf = new JLabel("PSF:");
		lblPsf.setBounds(6, 123, 46, 14);
		add(lblPsf);
		
		JLabel lblPfa = new JLabel("PFA :");
		lblPfa.setBounds(6, 151, 46, 14);
		add(lblPfa);
		
		pfaTF = new JTextField();
		pfaTF.setColumns(10);
		pfaTF.setBounds(58, 151, 86, 20);
		pfaTF.addKeyListener(this);
		add(pfaTF);
		lblPw.setBounds(6, 185, 46, 14);
		
		add(lblPw);
		pwTF.setColumns(10);
		pwTF.setBounds(58, 182, 86, 20);
		pwTF.addKeyListener(this);
		add(pwTF);
	}
	
	

	@Override
	public void mouseClicked(MouseEvent arg0) {
		JCheckBox tmp = (JCheckBox)arg0.getSource();
		if(tmp.equals(centerCB)){
			centerCB.setSelected(true);
			gaussianCB.setSelected(false);
			MyConstants.CUR_ALGORITHM = MyConstants.CENTER_OF_MASS;
			
		}else if(tmp.equals(gaussianCB)){
			centerCB.setSelected(false);
			gaussianCB.setSelected(true);
			MyConstants.CUR_ALGORITHM = MyConstants.GAUSSIAN_FITTING;
		}else if(tmp.equals(is3DCB)){
			if(tmp.isSelected()){
				MyConstants.IS_3D = true;
			}else{
				MyConstants.IS_3D = false;
			}
		}
		
	}


	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	public static AlgorithmEditor getInstance(){
		if(instance == null){
			instance = new AlgorithmEditor();
		}
		return instance;
	}



	@Override
	public void keyPressed(KeyEvent arg0) {
		JTextField tmp = (JTextField)arg0.getSource();
		if(arg0.getKeyCode() != KeyEvent.VK_ENTER){
			return;
		}
		if(tmp.equals(psfTF)){
			try{
				String value = psfTF.getText();
				if(value != null && !value.equals("")){
					double psf = Double.parseDouble(psfTF.getText());
					MyConstants.PSF = psf;
				}
			}catch(Exception e){
				IJ.error("Wrong input");
				e.printStackTrace();
			}
		}else if(tmp.equals(pfaTF)){
			try{
				String value = pfaTF.getText();
				if(value != null && !value.equals("")){
					double pfa = Double.parseDouble(pfaTF.getText());
					MyConstants.PFA = pfa;
				}
			}catch(Exception e){
				IJ.error("Wrong input");
				e.printStackTrace();
			}
		}else if(tmp.equals(pwTF)){
			try{
				String value = pwTF.getText();
				if(value != null && !value.equals("")){
					double pfa = Double.parseDouble(pwTF.getText());
					MyConstants.PW = pfa;
				}
			}catch(Exception e){
				IJ.error("Wrong input");
				e.printStackTrace();
			}
		}
		
	}



	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
